﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Business_Logic_Layer;
using System.Data;
using System.Collections;
using RealtorsPortal.Entities;
using RealtorsPortal.Data_Access_Layer;
using System.Web.Services;

namespace RealtorsPortal
{
    public partial class ManageLocations : System.Web.UI.Page
    {
        CountriesDAL cd = new CountriesDAL();
        RegionDAL rd = new RegionDAL();
        CitiesDAL cid = new CitiesDAL();
        DistrictsDAL dd = new DistrictsDAL();

        DataAccessHelper dah = new DataAccessHelper();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataCountry();
                LoadDataRegion();
                LoadDataCity();
                LoadDataDistrict();
                LoadDataCountryToDDLInRegionTab();

                LoadDataCountryToDDLInCityTab();
                //LoadDataRegionToDDLInCityTab();

                LoadDataCountryToDDLInDistrictTab();
                //LoadDataRegionToDDLInDistrictTab();
                //LoadDataCityToDDLInDistrictTab();

                ddlCountryForCity.Items.Insert(0,"----Select Country----");
                ddlCountryForDistrict.Items.Insert(0, "----Select Country----");
                ddlCountry.Items.Insert(0, "----Select Country----");
            }
        }

        protected void LoadDataCountryToDDLInRegionTab() {
            DataTable dt = new DataTable();
            dt = cd.getCountriesData();
            ddlCountry.DataSource = dt;
            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataBind();
        }

        protected void LoadDataCountryToDDLInDistrictTab()
        {
            DataTable dt = new DataTable();
            dt = cd.getCountriesData();
            ddlCountryForDistrict.DataSource = dt;
            ddlCountryForDistrict.DataValueField = "CountryId";
            ddlCountryForDistrict.DataTextField = "CountryName";
            ddlCountryForDistrict.DataBind();
        }

        protected void LoadDataCountryToDDLInCityTab()
        {
            DataTable dt = new DataTable();
            dt = cd.getCountriesData();
            ddlCountryForCity.DataSource = dt;
            ddlCountryForCity.DataValueField = "CountryId";
            ddlCountryForCity.DataTextField = "CountryName";
            ddlCountryForCity.DataBind();
        }

        protected void LoadDataRegionToDDLInCityTab(Region regionObj)
        {
            DataTable dt = new DataTable();
            dt = rd.GetRegionsByCountryId(regionObj);
            ddlRegion.DataSource = dt;
            ddlRegion.DataValueField = "RegionId";
            ddlRegion.DataTextField = "RegionName";
            ddlRegion.DataBind();
        }

        protected void LoadDataRegionToDDLInDistrictTab()
        {
            DataTable dt = new DataTable();
            dt = rd.getRegionsData();
            ddlRegionForDistrict.DataSource = dt;
            ddlRegionForDistrict.DataValueField = "RegionId";
            ddlRegionForDistrict.DataTextField = "RegionName";
            ddlRegionForDistrict.DataBind();
        }

        protected void LoadDataCityToDDLInDistrictTab()
        {
            DataTable dt = new DataTable();
            dt = cid.getCitiesData();
            ddlCity.DataSource = dt;
            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "CityName";
            ddlCity.DataBind();
        }

        protected void LoadDataCountry() {
            grvCountry.DataSource = cd.getCountriesData();
            grvCountry.DataBind();
        }

        
        protected void LoadDataRegion() {
            grvRegion.DataSource = rd.getRegionsDataWithCountry();
            grvRegion.DataBind();
        }

        protected void LoadDataCity() {
            grvCity.DataSource = cid.getCitiesDataWithRegions();
            grvCity.DataBind();
        }

        protected void LoadDataDistrict() {
            grvDistrict.DataSource = dd.getDistrictsDataWithCities();
            grvDistrict.DataBind();
        }

        protected void grvCountry_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCountry.PageIndex = e.NewPageIndex;
            this.LoadDataCountry();
        }

        protected void grvCountry_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvCountry.EditIndex = -1;
            LoadDataCountry();
        }

        protected void grvCountry_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "UpdateComponent":
                        this.UpdateCountry(index);
                        grvCountry.EditIndex = -1;
                        this.LoadDataCountry();
                        break;
                    case "CancelCompoent":
                        grvCountry.EditIndex = -1;
                        LoadDataCountry();
                        break;
                }
            }
        }

        private void UpdateCountry(int index)
        {
            Country ctObj = new Country();
            ctObj.CountryId = int.Parse(grvCountry.DataKeys[index].Value.ToString());
            ctObj.CountryName = ((TextBox)grvCountry.Rows[index].FindControl("etxtCountryName")).Text;
            cd.UpdateCountrys(ctObj);
            grvCountry.EditIndex = -1;
        }

        private void DeleteRowInCountryTab(int index)
        {
            int countryId = int.Parse(grvCountry.DataKeys[index].Value.ToString());
            Country countryObj = new Country();
            countryObj.CountryId = countryId;
            cd.DeleteCountrys(countryObj);
            grvCountry.EditIndex = -1;
        }

        protected void grvCountry_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.DeleteRowInCountryTab(e.RowIndex);
            grvCountry.EditIndex = -1;
            LoadDataCountry();
        }

        protected void grvCountry_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvCountry.EditIndex = e.NewEditIndex;
            LoadDataCountry();
        }

        protected void grvRegion_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvRegion.PageIndex = e.NewPageIndex;
            this.LoadDataRegion();
        }

        
        protected void grvRegion_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        private void UpdateRegion(int index)
        {
            Region regObj = new Region();
            regObj.CountryId = int.Parse(grvRegion.DataKeys[index].Value.ToString());
            regObj.RegionName = ((TextBox)grvRegion.Rows[index].FindControl("etxtRegionName")).Text;
            rd.UpdateRegions(regObj);
            grvRegion.EditIndex = -1;
        }

        protected void grvRegion_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvRegion.EditIndex = e.NewEditIndex;
            LoadDataRegion();
        }

        protected void grvRegion_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "UpdateComponent":
                        TextBox etxtRegionName = (TextBox)grvRegion.Rows[index].FindControl("etxtRegionName");
                        DropDownList extddlCountryName = (DropDownList)grvRegion.Rows[index].FindControl("extddlCountryName");
                        Region regObj = new Region();
                        regObj.RegionId = int.Parse(grvRegion.DataKeys[index].Value.ToString());
                        regObj.RegionName = etxtRegionName.Text;
                        regObj.CountryId = int.Parse(extddlCountryName.SelectedValue);
                        rd.UpdateRegions(regObj);
                        grvRegion.EditIndex = -1;
                        LoadDataRegion(); 
                        break;
                    case "CancelCompoent":
                        grvRegion.EditIndex = -1;
                        LoadDataRegion();
                        break;
                }
            }
        }

        protected void ddlPageSizeCountry_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grvCity_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void grvCity_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

        }

        protected void grvCity_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void grvCity_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void grvCity_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void btnAddCountry_Click(object sender, EventArgs e)
        {
            if (txtCountry.Text == "" || txtCountry.Text == null)
            {
                Utilities.Show("You have to enter Country Name!");
                return;
            }
            else
            {
                Country ctObj = new Country();
                ctObj.CountryName = txtCountry.Text;
                int result = cd.InsertCountrys(ctObj);
                txtCountry.Text = "";
                if (result > 0)
                {
                    Utilities.Show("Record Has been Saved in Database");
                    Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
                }
                else
                {
                    Utilities.Show("Record Has not been Saved in Database");
                    Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
                }
            }
        }

        protected void ddlPageSizeCountry_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void btnAddRegion_Click(object sender, EventArgs e)
        {
            Region rObj = new Region();
            rObj.RegionName = txtRegion.Text;
            rObj.CountryId = int.Parse(ddlCountry.DataValueField);
            int result = rd.InsertRegions(rObj);
            txtRegion.Text = "";
            if (result > 0)
            {
                Utilities.Show("Successful!");
            }
        }

        protected void btnAddCity_Click(object sender, EventArgs e)
        {
            City ctObj = new City();
            ctObj.CityName = txtCity.Text;
            ctObj.RegionId = int.Parse(ddlRegion.DataValueField);
            int result = cid.InsertCities(ctObj);
            txtCity.Text = "";
            if (result > 0)
            {
                Utilities.Show("Successful");
            }
            
        }

        protected void ddlCountryForCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void ddlRegion_Load(object sender, EventArgs e)
        {
            
            
        }

        protected void ddlCountryForCity_TextChanged(object sender, EventArgs e)
        {

        }

        protected void grvCountry_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbType");

                //if (cmbType != null)
                //{
                //    cmbType.DataSource = customer.FetchCustomerType();
                //    cmbType.DataBind();
                //    cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
                //}
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                //DropDownList cmbNewType = (DropDownList)e.Row.FindControl("cmbNewType");
                //cmbNewType.DataSource = customer.FetchCustomerType();
                //cmbNewType.DataBind();
            } 
        }

        protected void grvCountry_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox etxtCountryName = (TextBox)grvCountry.Rows[e.RowIndex].FindControl("etxtCountryName");
            Country countryObj = new Country();
            countryObj.CountryId = Convert.ToInt32(grvCountry.DataKeys[e.RowIndex].Value.ToString());
            countryObj.CountryName = etxtCountryName.Text;
            cd.UpdateCountrys(countryObj);
            grvCountry.EditIndex = -1;
            LoadDataCountry(); 
        }

        protected void grvCity_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList extddlRegionName = (DropDownList)e.Row.FindControl("extddlRegionName");

                if (extddlRegionName != null)
                {
                    extddlRegionName.DataSource = rd.getRegionsData();
                    extddlRegionName.DataBind();
                    extddlRegionName.SelectedValue = grvCity.DataKeys[e.Row.RowIndex].Values[0].ToString();
                }
            } 

        }

        protected void grvCity_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            grvCity.EditIndex = e.NewEditIndex;
            LoadDataCity(); 
        }

        protected void grvCity_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
        {
            grvCity.EditIndex = -1;
            LoadDataCity(); 
        }

        protected void grvCity_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox extxtCityName = (TextBox)grvCity.Rows[e.RowIndex].FindControl("extxtCityName");
            DropDownList extddlRegionName = (DropDownList)grvCity.Rows[e.RowIndex].FindControl("extddlRegionName");

            City cityObj = new City();
            cityObj.CityId = int.Parse(grvCity.DataKeys[e.RowIndex].Value.ToString());
            cityObj.CityName = extxtCityName.Text;
            cityObj.RegionId = int.Parse(extddlRegionName.SelectedValue);

            cid.UpdateCities(cityObj);
            grvCity.EditIndex = -1;
            LoadDataCity(); 
        }

        protected void grvCity_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
            City cityObj = new City();
            cityObj.CityId = int.Parse(grvCity.DataKeys[e.RowIndex].Values[0].ToString());
            cid.DeleteCities(cityObj);
            LoadDataCity(); 
        }

        protected void grvRegion_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox etxtRegionName = (TextBox)grvRegion.Rows[e.RowIndex].FindControl("etxtRegionName");
            DropDownList extddlCountryName = (DropDownList)grvRegion.Rows[e.RowIndex].FindControl("extddlCountryName");
            Region regObj = new Region();
            regObj.RegionId = int.Parse(grvRegion.DataKeys[e.RowIndex].Value.ToString());
            regObj.RegionName = etxtRegionName.Text;
            regObj.CountryId = int.Parse(extddlCountryName.SelectedValue);
            rd.UpdateRegions(regObj);
            grvRegion.EditIndex = -1;
            LoadDataRegion(); 
        }

        protected void grvRegion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Region regObj = new Region();
            regObj.RegionId = int.Parse(grvRegion.DataKeys[e.RowIndex].Values[0].ToString());
            rd.DeleteRegions(regObj);
            LoadDataRegion(); 
        }

        protected void grvRegion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList extddlCountryName = (DropDownList)e.Row.FindControl("extddlCountryName");

                if (extddlCountryName != null)
                {
                    extddlCountryName.DataSource = cd.getCountriesData();
                    extddlCountryName.DataBind();
                    extddlCountryName.SelectedValue = grvRegion.DataKeys[e.Row.RowIndex].Values[0].ToString();
                }
            }
        }

        protected void grvRegion_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvRegion.EditIndex = -1;
            LoadDataRegion(); 
        }

        protected void grvCity_RowCommand1(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "UpdateComponent":
                        TextBox extxtCityName = (TextBox)grvCity.Rows[index].FindControl("extxtCityName");
                        DropDownList extddlRegionName = (DropDownList)grvCity.Rows[index].FindControl("extddlRegionName");

                        City cityObj = new City();
                        cityObj.CityId = int.Parse(grvCity.DataKeys[index].Value.ToString());
                        cityObj.CityName = extxtCityName.Text;
                        cityObj.RegionId = int.Parse(extddlRegionName.SelectedValue);

                        cid.UpdateCities(cityObj);
                        grvCity.EditIndex = -1;
                        LoadDataCity();
                        break;
                    case "CancelCompoent":
                        grvCity.EditIndex = -1;
                        LoadDataCity(); 
                        break;
                }
            }
        }

        protected void grvDistrict_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList extddlCityName = (DropDownList)e.Row.FindControl("extddlCityName");

                if (extddlCityName != null)
                {
                    extddlCityName.DataSource = cid.getCitiesData();
                    extddlCityName.DataBind();
                    extddlCityName.SelectedValue = grvDistrict.DataKeys[e.Row.RowIndex].Values[0].ToString();
                }
            } 
        }

        protected void grvDistrict_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "UpdateComponent":
                        TextBox extxtDistrictName = (TextBox)grvDistrict.Rows[index].FindControl("extxtDistrictName");
                        DropDownList extddlCityName = (DropDownList)grvCity.Rows[index].FindControl("extddlCityName");

                        District disObj = new District();
                        disObj.DistrictId = int.Parse(grvDistrict.DataKeys[index].Value.ToString());
                        disObj.DistrictName = extxtDistrictName.Text;
                        disObj.CityId = int.Parse(extddlCityName.SelectedValue);

                        dd.UpdateDistricts(disObj);
                        grvDistrict.EditIndex = -1;
                        LoadDataDistrict();
                        break;
                    case "CancelCompoent":
                        grvDistrict.EditIndex = -1;
                        LoadDataDistrict();
                        break;
                }
            }
        }

        protected void grvDistrict_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvDistrict.EditIndex = e.NewEditIndex;
            LoadDataDistrict(); 
        }

        protected void grvDistrict_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvDistrict.EditIndex = -1;
            LoadDataDistrict(); 
        }

        protected void grvDistrict_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            District disObj = new District();
            disObj.DistrictId = int.Parse(grvDistrict.DataKeys[e.RowIndex].Values[0].ToString());
            dd.DeleteDistricts(disObj);
            LoadDataDistrict(); 
        }

        protected void grvDistrict_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox extxtDistrictName = (TextBox)grvDistrict.Rows[e.RowIndex].FindControl("extxtDistrictName");
            DropDownList extddlCityName = (DropDownList)grvDistrict.Rows[e.RowIndex].FindControl("extddlCityName");
            District disObj = new District();
            disObj.DistrictId = int.Parse(grvDistrict.DataKeys[e.RowIndex].Values[0].ToString());
            disObj.DistrictName = extxtDistrictName.Text;
            disObj.CityId = int.Parse(extddlCityName.SelectedValue);

            dd.UpdateDistricts(disObj);
            grvDistrict.EditIndex = -1;
            LoadDataDistrict(); 
        }

        protected void ddlCountryForCity_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Region regObj = new Region();
            regObj.CountryId = Convert.ToInt16(ddlCountryForCity.SelectedValue);
            LoadDataRegionToDDLInCityTab(regObj);
            ddlRegion.Items.Insert(0, "--Select Region--");
            if (ddlRegion.Items.Count > 1)
            {
                ddlRegion.Enabled = true;
            }
            else
            {
                ddlRegion.Enabled = false;
            }
        }

        protected void LoadDataRegionToDDLInDistrictTab(Region regionObj)
        {
            DataTable dt = new DataTable();
            dt = rd.GetRegionsByCountryId(regionObj);
            ddlRegion.DataSource = dt;
            ddlRegion.DataValueField = "RegionId";
            ddlRegion.DataTextField = "RegionName";
            ddlRegion.DataBind();
        }

        protected void ddlCountryForDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            Region regObj = new Region();
            regObj.CountryId = Convert.ToInt16(ddlCountryForDistrict.SelectedValue);
            LoadDataRegionToDDLInDistrictTab(regObj);
            ddlRegion.Items.Insert(0, "--Select Region--");
            if (ddlRegionForDistrict.Items.Count > 1)
            {
                ddlRegionForDistrict.Enabled = true;
            }
            else
            {
                ddlRegionForDistrict.Enabled = false;
            }
        }

    }
}